<?php

namespace app\model;

use think\Model;

class AuthUsers extends Model
{
    protected $table = 'auth_users';

    /**
     * 通过密码获取用户信息
     *
     * @param string $username
     * @param string $password
     * @return array
     */
    public function loginInfo(string $username, string $password)
    {
        $fields = [
            'id',
            'username',
            'token',
        ];
        $condition = [
            'username' => $username,
            'password' => $password,
        ];
        return $this
            ->where($condition)
            ->field($fields)
            ->findOrEmpty()
            ->toArray();
    }

    /**
     * 通过id更新token
     *
     * @param int $id
     * @param string $token
     * @return AuthUsers
     */
    public function updateTokenById(int $id, string $token)
    {
        return $this->where(['id' => $id])->limit(1)->update(['token' => $token]);
    }

    public function getInfoByToken(string $token)
    {
        $fields = [
            'id',
            'username',
        ];
        $condition = [
            'token' => $token,
        ];
        return $this
            ->where($condition)
            ->field($fields)
            ->findOrEmpty()
            ->toArray();
    }
}